# Contributor: omni <omni+alpine@hack.org>
# Maintainer: messense <messense@icloud.com>
pkgname=maturin
pkgver=1.4.0
pkgrel=0
pkgdesc="Build and publish crates with pyo3, rust-cpython and cffi bindings"
url="https://github.com/PyO3/maturin"
arch="all"
license="Apache-2.0 OR MIT"
makedepends="
	bzip2-dev
	cargo
	dbus-dev
	openssl-dev
	py3-gpep517
	py3-setuptools
	py3-setuptools-rust
	py3-wheel
	"
checkdepends="
	libffi-dev
	patchelf
	py3-cffi
	py3-pyo
	py3-virtualenv
	python3-dev
	"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	py3-$pkgname:py3:noarch
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/PyO3/maturin/archive/v$pkgver.tar.gz"
options="net"

_features="--features=full,native-tls"

case "$CARCH" in
s390x)
	# s390x fails to build password-storage, so don't append it
	;;
*)
	_features="$_features,password-storage"
	;;
esac

prepare() {
	default_prepare

	# Rust target triple.
	local target=$(rustc -vV | sed -n 's/host: //p')

	# Build against system-provided bz2.
	mkdir -p .cargo
	cat >> .cargo/config.toml <<-EOF
		[target.$target]
		zstd = { rustc-link-lib = ["bz2"] }
	EOF


	cargo fetch --target="$CTARGET" --locked
}

build() {
	export OPENSSL_NO_VENDOR=1

	export MATURIN_SETUP_ARGS="--no-default-features $_features"

	gpep517 build-wheel \
		--wheel-dir .dist \
		--output-fd 3 3>&1 >&2

	./target/release/maturin completions bash > $pkgname.bash
	./target/release/maturin completions fish > $pkgname.fish
	./target/release/maturin completions zsh > $pkgname.zsh
}

check() {
	export OPENSSL_NO_VENDOR=1

	cargo test \
	  --frozen \
	  --no-default-features $_features \
	  -- --skip git_sdist_generator
}

package() {
	install -Dm644 $pkgname.bash "$pkgdir"/usr/share/bash-completion/completions/$pkgname
	install -Dm644 $pkgname.fish "$pkgdir"/usr/share/fish/vendor_completions.d/$pkgname.fish
	install -Dm644 $pkgname.zsh "$pkgdir"/usr/share/zsh/site-functions/_$pkgname

	python3 -m installer -d "$pkgdir" \
		.dist/maturin*.whl
}

py3() {
	pkgdesc="$pkgdesc (python module)"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/lib/python3*
}

sha512sums="
c86afdeb694c2fe4b9b94a7bf5f657f9337d3b6179a3d430a26608353d4de7c463a3860e87225356cfe66f1111240717851173403c66a8c30ed3acf7c1d2afdb  maturin-1.4.0.tar.gz
"
